using Spring.Context;
using Spring.Context.Support;
using System;
using System.Runtime.Serialization;

namespace PostSharp4Spring
{
    [Serializable]
    internal sealed class ConfigurableImpl : IConfigurable
    {
        [NonSerialized]
        private IApplicationContext applicationContext;
        
        private readonly string name;

        private readonly object owner;

        public ConfigurableImpl( object owner, string name )
        {
            this.name = name;
            this.owner = owner;
            ApplicationContext.ConfigureObject( owner, name );
        }


        public IApplicationContext ApplicationContext { 
            get {
                if (null == this.applicationContext)
                {
                    this.applicationContext = ContextRegistry.GetContext();
                }
                return this.applicationContext;
            }
        }

        #region IDeserializationCallback Member

        public void OnDeserialization(object sender)
        {
            this.ApplicationContext.
                ConfigureObject(owner, name);
        }

        #endregion
    }
}